package com.cn.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cn.pojo.FileInfo;
import com.cn.pojo.TypeInfo;
import com.cn.service.FileService;
import com.cn.service.TypeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class FileController {
	@Autowired
	@Qualifier("typeService")
	TypeService typeService;
	@Autowired
	@Qualifier("fileService")
	FileService fileService;
	@RequestMapping(value="/consultIndex")
	public ModelAndView consultIndex(ModelAndView mv) {
		List<TypeInfo> type=typeService.findAll();
		mv.addObject("type",type);
		mv.setViewName("addFileForm");
		return mv;
	}
	@RequestMapping(value="/addFile.do")
	public ModelAndView addFile(ModelAndView mv,HttpServletRequest request,@RequestParam("index") Integer index,@RequestParam("file")MultipartFile file) {
		TypeInfo type=typeService.selectById(index);
		if(!file.isEmpty()) {
			String path=request.getServletContext().getRealPath("/")+type.getUrl();
			String fileName=file.getOriginalFilename();
			File filepath=new File(path,fileName);
			if(!filepath.getParentFile().exists()) {
				filepath.getParentFile().mkdirs();
			}
			try {
				file.transferTo(new File(path+File.separator+fileName));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			boolean addFile=fileService.addFile(fileName, type.getUrl()+"\\"+fileName, index);
			mv.setViewName("consultFileForm");
		}
		return mv;
	}
	@RequestMapping(value="/consultFile")
	public void consultFile(ObjectMapper mapper,HttpServletResponse response) {
		List<Map<String,Object>> file=fileService.consultFile();
		try {
			String jsonfromList = mapper.writeValueAsString(file);
			response.setCharacterEncoding("utf-8");
			response.setHeader("Access-Control-Allow-Origin", "*");
			try {
				response.getWriter().print(jsonfromList);
				response.getWriter().flush();
				response.getWriter().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value="/deleteFile.do")
	public ModelAndView deleteFile(ModelAndView mv,HttpServletRequest request,Integer id) {
		FileInfo file=fileService.consultFileById(id);
		File f=new File(request.getServletContext().getRealPath("/")+file.getUrl());
		if(f.exists()) {
			f.delete();
		}
		boolean deleteFile=fileService.deleteFile(id);
		mv.setViewName("consultFileForm");
		return mv;
	}
	@RequestMapping(value="/consultFileByType")
	public void consultFileByType(ModelAndView mv,ObjectMapper mapper,HttpServletResponse response,Integer typeId) {
		List<FileInfo> consultFileByType=fileService.consultFileByTypeId(typeId);
		try {
			String json=mapper.writeValueAsString(consultFileByType);
			response.setCharacterEncoding("utf-8");
			response.setHeader("Access-Control-Allow-Origin", "*");
			try {
				response.getWriter().print(json);
				response.getWriter().flush();
				response.getWriter().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
